package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SystemUserService systemUserService;

    @Override
    public CaptchaVo getCatchVo() {


        /**
         * 生产验证码图片 (生成图片,正确验证码存储到redis)
         *  1. 新的依赖需要导入
         *  2. 修改redis连接的配置 (application.yml)
         *  3. redisTemplate 配置四个序列化位置
         *  4. 生成验证码图片和正确验证码
         *  5. 正确验证码存储到redis里
         *  6. 把图片转成base64和rediskey一起装到CaptchaVo
         *  7. 返回CaptchaVo
         */
        //获取图片验证码
        SpecCaptcha specCaptcha = new SpecCaptcha(140,38,4);
        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);

        //将其存储到redis里面
        String code = specCaptcha.text().toLowerCase();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
        String image = specCaptcha.toBase64();
        stringRedisTemplate.opsForValue().set(key,code,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        return new CaptchaVo(image,key);
    }

    @Override
    public String getLoginVo(LoginVo loginVo) {
        //登陆提前验证验证码是否存在
        //验证验证码是否为空
        if (StringUtils.isBlank(loginVo.getCaptchaCode())) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        //验证验证码是否过期
        String code = stringRedisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if(StringUtils.isBlank(code)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //验证验证码内容是否正确
        if(!loginVo.getCaptchaCode().equalsIgnoreCase(code)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }

        //验证账号
        //验证账号是否存在
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername,loginVo.getUsername());
        SystemUser systemUserName = systemUserService.getOne(lambdaQueryWrapper);
        if (systemUserName==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR)  ;
        }
        //验证密码
        //验证密码是否正确
        String password = DigestUtils.md5Hex(loginVo.getPassword());
        if (!password.equals(loginVo.getPassword())){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //验证账号状态
        //验证账号是否可用
        if (systemUserName.getStatus()== BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //生成token
        //使用jwt生成token
        String token = JwtUtil.createToken(systemUserName.getId(), systemUserName.getName());
        //返回生成token
        return token;
    }
}
